﻿using UnityEngine;


public class BaseTank : MonoBehaviour
{
    /*
     *  基本坦克类，实现了坦克的移动，开火，死亡
     */
    public bool isPlayer;
    public float moveSpeed = 3;
    // 坦克的图像
    public Sprite[] tankSprite;
    // 炮弹的预设体（实例）
    public GameObject shellPrefab;
    // 爆炸的预设体（实例）
    public GameObject explosionPrefab;
    // 护盾的预设体（实例）
    public GameObject shieldPrefab;
    // 实例的图像渲染器
    protected SpriteRenderer sr;
    // 炮弹的欧拉角
    protected Vector3 shellEulerAngles;
    protected float fireTimeVal = 0;
    protected float moveTimeVal;

    protected bool isDefended = true;
    protected float defendTimeVal = 3;
    // 发动机音效播放器
    public AudioSource engineClipPlayer;
    public AudioSource eventClipPlayer;
    // 添加音效
    public AudioClip clip_fire;
    public AudioClip clip_die;
    public AudioClip clip_engineDriving;
    public AudioClip clip_engineDie;
    public AudioClip clip_explosion;
    public AudioClip clip_getBonus;
    public AudioClip clip_headquarterDestroyed;
    public AudioClip clip_hit;

    // 唤醒该实例
    private void Awake()
    {
        // 获取该实例的图像渲染器（此处是引用传递吗？）
        sr = GetComponent<SpriteRenderer>();
        // 初始化音乐播放器
        engineClipPlayer = gameObject.AddComponent<AudioSource>();
        eventClipPlayer = gameObject.AddComponent<AudioSource>();
        // 发动机音效启动
        engineClipPlayer.clip = clip_engineDriving;
        engineClipPlayer.loop = true;
        engineClipPlayer.volume = 0.2f;
        engineClipPlayer.Play();
    }
    // 每帧执行一次该函数
    void FixedUpdate()
    {
        if (isDefended)
        {
            shieldPrefab.SetActive(true);
            defendTimeVal -= Time.deltaTime;
            if (defendTimeVal <= 0)
            {
                isDefended = false;
                shieldPrefab.SetActive(false);
            }
        }
        Move();
        Fire();
    }

    protected void Fire()
    {
        if (fireTimeVal > 0.4)
        {
            if (isPlayer)
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    // 创建炮弹的实例
                    GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.Euler(shellEulerAngles));
                    // 为炮弹指定它爹，避免开炮的时候炸膛
                    shell.GetComponent<Shell>().parentID = this.GetInstanceID();
                    fireTimeVal = 0;
                    eventClipPlayer.clip = clip_fire;
                    eventClipPlayer.volume = 0.2f;
                    eventClipPlayer.Play();
                }
            }
            else
            {
                GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.Euler(shellEulerAngles));
                shell.GetComponent<Shell>().parentID = this.GetInstanceID();
                fireTimeVal = 0;
                eventClipPlayer.clip = clip_fire;
                eventClipPlayer.volume = 0.2f;
                eventClipPlayer.Play();
            }
        }
        else
        {
            fireTimeVal += Time.deltaTime;
        }
    }

    // 根据移动命令执行移动
    private void ExecuteMove(float v, float h)
    {
        // 为了避免两个键同时按下的情况，此处只处理一个键，另一个被按下的键丢弃
        if (h != 0)
        {
            if (h < 0)
            {
                shellEulerAngles = new Vector3(0, 0, 90);
                transform.eulerAngles = new Vector3(0, 0, 90);
            }
            else if (h > 0)
            {
                shellEulerAngles = new Vector3(0, 0, 270);
                transform.eulerAngles = new Vector3(0, 0, 270);
            }
            transform.Translate(Vector3.up * moveSpeed * Time.fixedDeltaTime, Space.Self);
        }
        else if (v != 0)
        {
            if (v < 0)
            {
                shellEulerAngles = new Vector3(0, 0, 180);
                transform.eulerAngles = new Vector3(0, 0, 180);
            }
            else if (v > 0)
            {
                shellEulerAngles = new Vector3(0, 0, 0);
                transform.eulerAngles = new Vector3(0, 0, 0);
            }
            transform.Translate(Vector3.up * moveSpeed * Time.fixedDeltaTime, Space.Self);
        }
    }

    // 生成AI运动指令
    private void Get_AI_Motion(ref float h, ref float v)
    {
        int num = Random.Range(0, 4);
        switch (num)
        {
            case 0:
                h = 0;
                v = -1;
                break;
            case 1:
                h = 0;
                v = +1;
                break;
            case 2:
                h = -1;
                v = 0;
                break;
            case 3:
                h = +1;
                v = 0;
                break;
        }
    }

    protected virtual void Move()
    {
        // 第一步，获得运动指令
        // 第二步，执行运动
        float h = 0;
        float v = 0;

        if (isPlayer)
        {
            h = Input.GetAxisRaw("Horizontal");
            v = Input.GetAxisRaw("Vertical");
        }
        else
        {
            if (moveTimeVal > 1)
            {
                Get_AI_Motion(ref h, ref v);
                moveTimeVal = 0;
            }
            else
            {
                // 如果冷却未结束，按当前方向移动
                float angle = transform.eulerAngles.z;
                switch (angle)
                {
                    case 0:
                        h = 0;
                        v = 1;
                        break;
                    case 180:
                        h = 0;
                        v = -1;
                        break;
                    case 90:
                        h = -1;
                        v = 0;
                        break;
                    case 270:
                        h = 1;
                        v = 0;
                        break;
                }
                moveTimeVal += Time.deltaTime;
            }
        }
        ExecuteMove(v, h);
    }

    protected void Die()
    {
        // 创建爆炸实例
        Instantiate(explosionPrefab, transform.position, transform.rotation);
        AudioSource.PlayClipAtPoint(clip_die, gameObject.transform.position, 0.3f);

        //engineClipPlayer.clip = clip_engineDie;
        //engineClipPlayer.Play();        
        // 销毁该实例
        Destroy(gameObject);

    }
}
